package SPAN::Method;

use warnings;
use strict;

use Reaction::Class;

class Method is 'SPAN::InterfaceModel::Action', which {
    implements moniker => as {
        my ($self) = @_;

        my $class = ref($self) || $self;
        ( my $moniker = $class ) =~ s/SPAN\:\:Method\:\://g;
        $moniker =~ s/\:\:/_/;
        return lc $moniker;
    }

    implements do_apply => as {
        my ($self) = @_;
        my $params = $self->parameter_hashref;
        my $method = eval { $self->target_model->new(%$params) };
        if($@) {
            my $error = $@;
            die $error unless ref($error);
            return { error => $$error, params => $params };
        }
        my $value  = $method->run;
        my %data = ( value => $value, params => $params );
        if ( my $call = $method->can('partial_results') ) {
            $data{partial_results} = $call->($method);
        }
        return \%data;
    };
};

1;
